import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from '../../environments/environment';
import { Observable } from 'rxjs';
import { UnifyResponse } from '../../common/response';


export interface LoginDto {
  code: string;
  password: string;
  username: string;
  uuid: string;
  rememberMe?: boolean
}


export interface LoginResponse {
  token: string;
}

export interface ImgCodeResponse {
  captchaEnabled: boolean,
  img: string,
  uuid: string
}

const baseUrl = environment.baseApiUrl

@Injectable({
  providedIn: 'root'
})
export class LoginService {

  constructor(
    private http: HttpClient
  ) { }

  login(data: LoginDto): Observable<UnifyResponse<LoginResponse>> {
    return this.http.post<UnifyResponse<LoginResponse>>(`${baseUrl}/login`, data);
  }

  getCodeImg() : Observable<UnifyResponse<ImgCodeResponse>> {
    return this.http.get<UnifyResponse<ImgCodeResponse>>(`${baseUrl}/captchaImage`)
  }


  logout() {
    return this.http.post<UnifyResponse<null>>(`${baseUrl}/logout`, null) 
  }

}
